package cn.cs.controller;

import cn.cs.common.DateUtil;
import cn.cs.common.Dto;
import cn.cs.common.DtoUtil;
import cn.cs.common.IdWorker;
import cn.cs.pojo.RepairComplaint;
import cn.cs.pojo.RepairComplaintNode;
import cn.cs.service.RepairComplaintService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/*投诉接口*/
@RequestMapping("/repairComplaint")
@RestController
public class RepairComplaintController {

    @Resource
    private RepairComplaintService repairComplaintService;

    /**
     * 平台客服接到投诉电话，添加投诉
     * @param complaint
     * @return
     * @throws Exception
     */
    @RequestMapping("/csPlatform/saveComplaint")
    @ResponseBody
    public Dto saveComplaint(@RequestBody RepairComplaint complaint) throws Exception{
        String id = IdWorker.getId();
        complaint.setId(id);
        complaint.setState("0");
        complaint.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        complaint.setUpdateTime(complaint.getCreateTime());
        return DtoUtil.returnSuccess("投诉添加成功", repairComplaintService.addRepairComplaint(complaint));
    }

    /**
     * 分页查询投诉列表
     * @param pageNumber
     * @param pageSize
     * @param phone
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/getComplaintPage")
    public Dto getComplaintPage(Integer pageNumber, Integer pageSize, String phone, String personName) throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("phone", phone);
        param.put("personName", personName);
        return DtoUtil.returnSuccess("获取成功", repairComplaintService.queryRepairComplaintPageByMap(param, pageNumber, pageSize));
    }

    /**
     * 获取投诉处理内容子表
     * @param complaintId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/getComplaintNode")
    public Dto getComplaintNode(String complaintId) throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("complaintId", complaintId);
        return DtoUtil.returnSuccess("获取成功", repairComplaintService.getRepairComplaintNodeListByMap(param));
    }


    /**
     * 添加处理内容
     * @param complaintNode
     * @return
     * @throws Exception
     */
    @RequestMapping("/csPlatform/saveComplaintNode")
    @ResponseBody
    @Transactional
    public Dto saveComplaint(@RequestBody RepairComplaintNode complaintNode) throws Exception{
        String id = IdWorker.getId();
        String delwith_user = complaintNode.getDelwithUserId();
        Map<String, Object> map = repairComplaintService.getPersonByCode(delwith_user);
        complaintNode.setDelwithUserId(map.get("id").toString());
        complaintNode.setDelTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        complaintNode.setId(id);
        RepairComplaint repairComplaint = new RepairComplaint();
        repairComplaint.setId(complaintNode.getComplaintId());
        repairComplaint.setState(complaintNode.getState());
        repairComplaintService.modifyRepairComplaint(repairComplaint);

        return DtoUtil.returnSuccess("添加成功", repairComplaintService.addRepairComplaintNode(complaintNode));
    }


}
